//
//  IntroduceController.m
//  BeautyClother
//
//  Created by dllo on 15/9/28.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "IntroduceController.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "IntroOneCell.h"
#import "womenDetailViewController.h"
@interface IntroduceController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) NSMutableDictionary *dic;

@end

@implementation IntroduceController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic = [NSMutableDictionary dictionary];
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSLog(@"%@", self.Inid);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
    
    [self handleData];
    [self FlowData];
    [self createTable];

}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleData
{
    NSDictionary *dic = @{@"ga":@"%2Fsku",
                          @"gv":@"633",
                          @"id":self.Inid,
                          @"access_token":@"",
                          @"gi":@"359776051231449",
                          @"gos":@"6.3.3",
                          @"gsv":@"4.3",
                          @"p":@"GT-I9300I",
                          @"gc":@"qihu360",
                          @"gn":@"mxyc_adr",
                          @"gs":@"720x1280",
                          @"gf":@"android"};
    
    [AsyncConnection asyncConnection:Intro_Head parmaters:dic block:^(NSData *data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dic = [result objectForKey:@"data"];
        [self.table reloadData];
    } path:@"IntroHead"];
}

- (void)FlowData
{
    NSDictionary *dic = @{@"width":@"720",
                          @"source":@"tmall",
                          @"ga":@"%2Fsku_imgs",
                          @"gv":@"633",
                          @"gi":@"359776051231449",
                          @"access_token":@"",
                          @"gos":@"6.3.3",
                          @"gsv":@"4.3",
                          @"p":@"GT-I9300I",
                          @"gc":@"qihu360",
                          @"source_id":self.source_id,
                          @"gn":@"mxyc_adr",
                          @"gs":@"720x1280",
                          @"gf":@"android"};
    
    [AsyncConnection asyncConnection:Intro_Flow parmaters:dic block:^(NSData *data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [[result objectForKey:@"data"] objectForKey:@"items"];
        [self.table reloadData];
    } path:@"IntroFlow"];
    
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_table];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.backgroundColor = [UIColor whiteColor];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_table release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else{
    return (self.array.count + 1);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        IntroOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"one"];
        if (!cell) {
            cell = [[IntroOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"one"];
        }
        cell.price.text = [NSString stringWithFormat:@"￥%@", [self.dic objectForKey:@"price"]];
        cell.desc.text = [self.dic objectForKey:@"description"];
        return cell;
    }
    else{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld,%ld", indexPath.section, indexPath.row]];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld,%ld", indexPath.section, indexPath.row]];
    }
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.frame.size.width - 10, cell.frame.size.height - 10)];
        NSString *pic = [[[self.array objectAtIndex:indexPath.row - 1] objectForKey:@"component"] objectForKey:@"picUrl"];
        pic = [[pic componentsSeparatedByString:@"?"] objectAtIndex:0];
        [image sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        [cell addSubview:image];
    
       return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
    UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableCellWithIdentifier:@"header"];
    
    CGFloat width = [[self.dic objectForKey:@"width"] floatValue];
    CGFloat height = [[self.dic objectForKey:@"height"] floatValue];

    if (myHeader == nil) {
        myHeader = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"header"];
        
    }
    UIImageView *ii = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * height / width)];
    ii.userInteractionEnabled = YES;
    [myHeader addSubview:ii];
    NSString *pic = [self.dic objectForKey:@"normalPicUrl"];
    pic = [[pic componentsSeparatedByString:@"?"] objectAtIndex:0];
    [ii sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    
    
    return myHeader;
    } else{
        return [[[UITableViewHeaderFooterView alloc]init]autorelease];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
    CGFloat width = [[self.dic objectForKey:@"width"] floatValue];
    CGFloat height = [[self.dic objectForKey:@"height"] floatValue];
    return self.view.frame.size.width * height / width;
    } else{
        return 5;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 80;
        } else
        {
            CGFloat width = [[[self.array objectAtIndex:indexPath.row - 1] objectForKey:@"width"] floatValue];
            CGFloat height = [[[self.array objectAtIndex:indexPath.row - 1] objectForKey:@"height"] floatValue];
            return (self.view.frame.size.width -  10) * height / width + 20;
        }
    } else
    {
        return 0;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
